//
//  JZUpdateViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/10/25.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "JZUpdateViewController.h"

#import "JZAddView.h"
@interface JZUpdateViewController ()<UIPickerViewDelegate,UIPickerViewDataSource,UITextViewDelegate>{
    JZAddView *addView;
    UIView *bottomView;
    UIView *bottomView2;
    UIView *bottomView3;
    UIButton *btn;
    NSArray *arr;
}

@property (nonatomic,strong) NSMutableArray *array1;
@property (nonatomic,strong) NSMutableArray *array2;

@property (nonatomic,strong) UIPickerView *pickerView1;
@property (nonatomic,strong) UIPickerView *pickerView2;
@property (nonatomic,strong) UIPickerView *pickerView3;

@property (nonatomic,copy) NSString *serviceId;//类别id
@property (nonatomic,copy) NSString *serviceDic;//方向id

@end

@implementation JZUpdateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    _array1 = [NSMutableArray array];
    _array2 = [NSMutableArray array];
    arr = @[@"元",@"元/时",@"元/月"];
    
    [self showNav:@"修改商品"];
    [self loadUI];
    [self loadData];
    
}

-(void)textViewDidChange:(UITextView *)textView{
    //限制长度
    if (textView.text.length >30) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 30)];
        [textView.undoManager removeAllActions];
        [textView becomeFirstResponder];
        return;
    }
}

- (void)loadUI{
    UIScrollView *sclView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    sclView.contentSize = CGSizeMake(ScreenWidth, 600);
    sclView.backgroundColor =  COLOR_RGB(242, 242, 242, 1);
    [self.view addSubview:sclView];
    
    NSString *classname = [_dic objectForKey:@"classname"];
    //
    _serviceId = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"classid"] integerValue]];
    _serviceDic = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"directionid"] integerValue]];
    
    if ([classname isEqualToString:@"家政"]) {
        addView = [[[NSBundle mainBundle] loadNibNamed:@"JZAddView" owner:nil options:nil] lastObject];
        addView.frame = CGRectMake(0, 0, ScreenWidth, 290);
        [sclView addSubview:addView];
        
        addView.textIntro.layer.cornerRadius = 5.0;
        addView.textIntro.layer.borderWidth = 1.0;
        addView.textIntro.layer.borderColor = COLOR_RGB(242, 242, 242, 1).CGColor;
        
        addView.labType.text = classname;
        addView.textIntro.text = [_dic objectForKey:@"synopsis"];
        addView.textPrice.text = [_dic objectForKey:@"price"];
        addView.textArea.text = [_dic objectForKey:@"proportion"];
        addView.labSelDic.text = [_dic objectForKey:@"directionidname"];
        
        addView.textIntro.delegate = self;
        addView.labLine.hidden = YES;
        addView.labDic.hidden = YES;
        addView.labSelDic.hidden = YES;
        addView.textArea.hidden = YES;
        addView.labArea.hidden = YES;
        addView.images.hidden = YES;
        
        
    }else{
        addView = [[[NSBundle mainBundle] loadNibNamed:@"JZAddView" owner:nil options:nil] lastObject];
        addView.frame = CGRectMake(0, 0, ScreenWidth, 183);
        [sclView addSubview:addView];
        
        addView.textIntro.layer.cornerRadius = 5.0;
        addView.textIntro.layer.borderWidth = 1.0;
        addView.textIntro.layer.borderColor = COLOR_RGB(242, 242, 242, 1).CGColor;
        
        addView.labType.text = classname;
        addView.textIntro.text = [_dic objectForKey:@"synopsis"];
        addView.textPrice.text = [_dic objectForKey:@"price"];
        
        addView.textIntro.delegate = self;
        addView.labLine.hidden = YES;
        addView.labDic.hidden = YES;
        addView.labSelDic.hidden = YES;
        addView.textArea.hidden = YES;
        addView.labArea.hidden = YES;
        addView.images.hidden = YES;
    }
    
    
    
    addView.labType.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click1)];
    [addView.labType addGestureRecognizer:tap1];
    
    [addView.btnSelPrice addTarget:self action:@selector(clickPrice) forControlEvents:UIControlEventTouchUpInside];
    
    addView.labSelDic.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click2)];
    [addView.labSelDic addGestureRecognizer:tap2];
    
    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(20, addView.frame.origin.y+addView.frame.size.height+20, ScreenWidth-40, 45);
    [btn setTitle:@"修改" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.backgroundColor = COLOR_RGB(253, 17, 66, 1);
    btn.layer.cornerRadius = 5.0;
    [btn addTarget:self action:@selector(clickAdd) forControlEvents:UIControlEventTouchUpInside];
    [sclView addSubview:btn];
    
    
}

- (void)click1{
    bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-250, ScreenWidth, 250)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(10, 0, 40, 40);
    [btn1 setTitle:@"取消" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(ScreenWidth-50, 0, 40, 40);
    [btn2 setTitle:@"确定" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(clickSure) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:btn2];
    
    _pickerView1 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 210)];
    _pickerView1.dataSource = self;
    _pickerView1.delegate = self;
    [bottomView addSubview:_pickerView1];
}

- (void)clickCancel{
    [bottomView removeFromSuperview];
    [bottomView2 removeFromSuperview];
    [bottomView3 removeFromSuperview];
}

- (void)clickSure{
    [bottomView removeFromSuperview];
    [bottomView2 removeFromSuperview];
    [bottomView3 removeFromSuperview];
    if ([addView.labType.text isEqualToString:@"家政"]) {
        addView.frame = CGRectMake(0, 0, ScreenWidth, 290);
        addView.labLine.hidden = NO;
        addView.labDic.hidden = NO;
        addView.labSelDic.hidden = NO;
        addView.textArea.hidden = NO;
        addView.labArea.hidden = NO;
        addView.images.hidden = NO;
        btn.frame = CGRectMake(20, addView.frame.origin.y+addView.frame.size.height+20, ScreenWidth-40, 45);
    }else{
        addView.frame = CGRectMake(0, 0, ScreenWidth, 183);
        
        addView.labSelDic.hidden = YES;
        addView.textArea.hidden = YES;
        addView.labArea.hidden = YES;
        addView.labDic.hidden = YES;
        addView.labLine.hidden = YES;
        addView.images.hidden = YES;
        btn.frame = CGRectMake(20, addView.frame.origin.y+addView.frame.size.height+20, ScreenWidth-40, 45);
    }
    
}

- (void)clickPrice{
    bottomView2 = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-250, ScreenWidth, 250)];
    bottomView2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView2];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(10, 0, 40, 40);
    [btn1 setTitle:@"取消" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(clickCancel2) forControlEvents:UIControlEventTouchUpInside];
    [bottomView2 addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(ScreenWidth-50, 0, 40, 40);
    [btn2 setTitle:@"确定" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(clickSure2) forControlEvents:UIControlEventTouchUpInside];
    [bottomView2 addSubview:btn2];
    
    _pickerView2 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 210)];
    _pickerView2.dataSource = self;
    _pickerView2.delegate = self;
    [bottomView2 addSubview:_pickerView2];
    
}

- (void)clickCancel2{
    [bottomView removeFromSuperview];
    [bottomView2 removeFromSuperview];
    [bottomView3 removeFromSuperview];
}

- (void)clickSure2{
    [bottomView removeFromSuperview];
    [bottomView2 removeFromSuperview];
    [bottomView3 removeFromSuperview];
}
//服务方向
- (void)click2{
    bottomView3 = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-250, ScreenWidth, 250)];
    bottomView3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView3];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(10, 0, 40, 40);
    [btn1 setTitle:@"取消" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(clickCancel3) forControlEvents:UIControlEventTouchUpInside];
    [bottomView3 addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(ScreenWidth-50, 0, 40, 40);
    [btn2 setTitle:@"确定" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(clickSure3) forControlEvents:UIControlEventTouchUpInside];
    [bottomView3 addSubview:btn2];
    
    _pickerView3 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 210)];
    _pickerView3.dataSource = self;
    _pickerView3.delegate = self;
    [bottomView3 addSubview:_pickerView3];
}

- (void)clickCancel3{
    [bottomView removeFromSuperview];
    [bottomView2 removeFromSuperview];
    [bottomView3 removeFromSuperview];
}

- (void)clickSure3{
    [bottomView removeFromSuperview];
    [bottomView2 removeFromSuperview];
    [bottomView3 removeFromSuperview];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (pickerView == _pickerView1) {
        return _array1.count;
    }else if (pickerView == _pickerView2){
        return arr.count;
    }else{
        return _array2.count;
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView == _pickerView1) {
        NSString *str = [_array1[row] objectForKey:@"name"];
        
        return str;
    }else if (pickerView == _pickerView2){
        NSString *str = arr[row];
        
        return str;
    }else{
        NSString *str = [_array2[row] objectForKey:@"name"];
        return str;
    }
    
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (pickerView == _pickerView1) {
        NSString *str = [_array1[row] objectForKey:@"name"];
        NSString *strid = [_array1[row] objectForKey:@"id"];
        addView.labType.text = str;
        _serviceId = strid;
    }else if (pickerView == _pickerView2){
        NSString *str = arr[row];
        [addView.btnSelPrice setTitle:str forState:UIControlStateNormal];
    }else{
        NSString *str = [_array2[row] objectForKey:@"name"];
        NSString *strid = [_array2[row] objectForKey:@"id"];
        addView.labSelDic.text = str;
        _serviceDic = strid;
        
    }
    
}
//家政-修改服务
- (void)clickAdd{
    if (addView.textIntro.text.length == 0) {
        [LICGToast showWithText:@"请输入服务简介"];
        return;
    }else if (addView.textPrice.text.length == 0){
        [LICGToast showWithText:@"服务价格不能为空"];
        return;
    }
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *cpid = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"id"] integerValue]];
    _serviceId = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"classid"] integerValue]];
    _serviceDic = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"directionid"] integerValue]];
    
    [parmas setObject:cpid forKey:@"id"];
    [parmas setObject:_serviceId forKey:@"classid"];
    [parmas setObject:_serviceDic forKey:@"directionid"];
    [parmas setObject:addView.textIntro.text forKey:@"synopsis"];
    [parmas setObject:addView.textPrice.text forKey:@"price"];
    NSString *classname = [_dic objectForKey:@"classname"];
    if ([classname isEqualToString:@"家政"]) {
        [parmas setObject:addView.btnSelPrice.titleLabel.text forKey:@"servicetime"];
        [parmas setObject:addView.textArea.text forKey:@"proportion"];
    }
    
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Housekeeping/update",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"修改家政res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

//家政-类型与方向
- (void)loadData{
    NSString *url = [NSString stringWithFormat:@"%@/App/Housekeeping/genre",urlHead];
    [[AFHTTPSessionManager manager] GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            _array1 = [[responseObject objectForKey:@"result"] objectForKey:@"classification"];
            _array2 = [[responseObject objectForKey:@"result"] objectForKey:@"direction"];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
